package org.example.movies.controller;

import com.alibaba.fastjson.JSONObject;
import org.example.movies.pojo.TUser;
import org.example.movies.service.IUserService;
import org.example.movies.service.impl.UserServiceImpl;
import org.example.movies.utils.ResponseEntity;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/LoginController")
public class LoginController extends HttpServlet {
    private IUserService service=new UserServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ResponseEntity resp=new ResponseEntity();
        String username = request.getParameter("username");
        String password=request.getParameter("password");
        String code = request.getParameter("code");
        String serverCode = (String) request.getSession().getAttribute("login");
        if (serverCode.equals(code)){
            boolean flag = service.checkUser(new TUser(username, password));
            if (flag){
                resp.setMsg("登录成功");
                resp.setStatus(200);
                resp.setData(flag);
            }else {
                resp.setMsg("用户名或者密码错误");
                resp.setStatus(404);
            }
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().println(JSONObject.toJSONString(resp));
        }else {
            resp.setStatus(201);
            resp.setMsg("验证码错误");
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().println(JSONObject.toJSONString(resp));
        }
    }
}
